home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
090
/
byte0387.arc
/
IMAGEIO.C
< prev
next >
Wrap
C/C++ Source or Header
|
1985-07-12
|
7KB
|
196 lines
/*
* imageio.c
*
* These routines provide the base level image I/O routines. There is a
* readimage() and writeimage() routine. Both require only a pointer to
* a memory array and a pointer to a file name.
*
*/
#include <exec/types.h> /* The UBYTE and USHORT types are here */
#include <stdio.h> /* The standard C I/O functions */
#include <fcntl.h> /* The Level 1 file I/O constants */
/*
* Function : SetPixel
* This function will set a pixel in the image to the given value.
* It is passed four values; the first is a pointer to the
* image array, the second through fourth are the row, column and value
* of the pixel, which are all integers.
*/
void
SetPixel(image,col,row,val)
UBYTE image[]; /* An array of pixels */
int col, /* The pixel x coordinate or column */
row, /* The pixel y coordinate or row */
val; /* a value between 0 and 15 for the pixel */
{
int temp, /* a temporary value */
index, /* The index into the array */
shift; /* shift factor (4 if pixel even, 0 if it is odd) */
/* Since two pixels are contained in a byte the 640 pixel line is really
* 320 bytes wide, and the column value divided by two is the byte containing
* the pixel we want. If column is even the pixel is in the left half of the
* byte and if column is odd the pixel is in the right half.
*/
shift = (col % 2) ? 0 : 4 ;
index = (row * 320) + (col / 2);
temp = val << shift;
if ((index < 0) || (index > 127999)) /* Index checking */
printf("Error! Bad row and column passed to SetPixel.\n");
else
image[index] = (image[index] & (0x0f0 >> shift)) + temp;
}
/*
* Function : Pixel
* This function will return the value of the pixel in the image. It is
* passed four values; the first is a pointer to the image array, the
* second through fourth are the row, column and value of the pixel, which
* are all integers.
*
*/
int
Pixel(image,col,row)
UBYTE image[]; /* An array of pixels */
int col, /* The pixel x coordinate or column */
row; /* The pixel y coordinate or row */
{
int temp, /* a temporary value */
index, /* The index into the array */
shift; /* shift factor (4 if pixel even, 0 if it is odd) */
/* This is the same calculation as in SetPixel above */
temp = 0;
shift = (col % 2) ? 0 : 4 ;
index = (row * 320) + (col / 2);
if ((index < 0) || (index > 127999)) /* Index checking */
printf("Error! Illegal values passed to Pixel(%d,%d).\n",col,row);
else
temp = (image[index] >> shift) & 0x0f;
return(temp);
}
/*
* Function : ReadImage
* This function will read in the 4 bitplanes from the file specified and
* store them as 4 bit pixels in the image array. It returns zero if it
* was successful, and a negative number if it detected an error.
* Errors include :
* -1 Couldn't open 'filename'
* -2 Didn't read enough pixel data.
* -3 Didn't read enough colormap data.
*/
int
ReadImage(filename, image, colormap)
char *filename; /* A pointer to a filename string */
UBYTE *image; /* A pointer to a 128000 byte array */
USHORT *colormap; /* An array of color map entries */
{
short i,j,k;
int n,fh, /* Byte count, File Handle */
error, /* indicator that an error occurred during read */
ishft,pshft; /* Some shift factors for manipulating bits. */
UBYTE pixels[80]; /* 640 bits worth (one line) of pixel data. */
/* Open the input file */
fh = open(filename,O_RDONLY);
if (fh == -1) return(-1); /* Return error if it couldn't be opened */
error = 0;
printf("Reading in source image from file %s ... ",filename);
for (i=0; i<4; i++) { /* Four bit planes */
for (k=0; k<400; k++) { /* 400 lines */
n = read(fh,pixels,80); /* Read in a row of pixels */
if (n == 80) {
for (j=0; j<640; j++) { /* Unpack the pixels */
ishft = (j % 2) ? i+4 : i; /* the image's byte shift factor */
pshft = (7-j%8); /* The pixel's byte shift factor */
*(image+(j/2)+(k*320)) &= ~(1 << ishft); /* Clear old bit */
*(image+(j/2)+(k*320)) |= ((pixels[j>>3] >> pshft) & 1) << ishft;
}
}
else error = -1; /* If we didn't get enough data it is an error */
}
}
if (error == 0) {
j = read(fh,(UBYTE *)colormap,32); /* Read in the color map */
if (j != 32) error = -1;
}
printf("Done.\n");
close(fh);
return(error);
}
/*
* Function : WriteImage
* This function will write out the image array to the specified file. It
* converts the 4 bit/pixel format of the image array to the bit plane
* format used by the iff conversion programs. It returns zero if it was
* successful and a negative number if it detected an error.
* Errors include :
* -1 Couldn't open file
* -2 Unexpected EOF (probably the disk was full)
* -3 Unexpected EOF while writing colormap
*/
int
WriteImage(filename, image, colormap)
char *filename; /* A pointer to a filename string */
UBYTE *image; /* A pointer to a 128000 byte array */
USHORT colormap[]; /* An array of color map entries */
{
short i,j,k;
int n,fh, /* byte count, File Handle */
error, /* indicator that an error occurred during read */
ishft,pshft; /* Some shift factors for manipulating bits. */
UBYTE pixels[80]; /* 640 bits worth (one line) of pixel data. */
/* First we open the file */
fh = open(filename,O_WRONLY+O_CREAT); /* Write only, and create it */
if (fh == -1) return(-1);
error = 0;
/* Now write out the image */
printf("Writing the processed image to file %s ... ",filename);
for (i=0; i<4; i++) { /* Four bit planes */
for (k=0; k<400; k++) { /* 400 lines */
for (j=0; j<640; j++) { /* Unpack the pixels */
ishft = (j % 2) ? 4+i : i; /* The bit number in the image byte */
pshft = 7 - (j % 8); /* Pixel shift value for pixels array */
pixels[j>>3] &= ~(1 << pshft); /* clear previous bit */
pixels[j>>3] |= ((*(image+(j/2)+(k*320)) >> ishft) & 1) << pshft;
}
n = write(fh,pixels,80); /* Write out this line of bits */
if (n < 80) error = -2;
}
}
if (error == 0) {
j = write(fh,(UBYTE *)colormap,32); /* write out the color map */
if (j < 32) error = -3;
}
printf("Done.\n");
close(fh); /* Clean up after ourselves */
return(error); /* And return */
}